package com.game.sdk.db.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.game.sdk.db.DBHelper;
import com.game.sdk.domain.AgentDbBean;

/**
 * Created by liu hong liang on 2016/9/29.
 */

public class AgentDbDao {
    public static final String AGENT_TABLE_NAME="tagent";


    private static AgentDbDao agentDbDao;
    private DBHelper dbHelper;
    private AgentDbDao(Context context){
        dbHelper = new DBHelper(context, null, 2);
    }
    public synchronized static AgentDbDao  getInstance(Context context){
        if(agentDbDao==null){
            agentDbDao=new AgentDbDao(context);
        }
        return agentDbDao;
    }
    public void addOrUpdate(AgentDbBean agentDbBean){
        SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from " + AGENT_TABLE_NAME + " where packageName=?", new String[]{agentDbBean.getPackageName()});
        AgentDbBean oldAgentDbBean=null;
        while (cursor.moveToNext()){
            oldAgentDbBean=new AgentDbBean();
            oldAgentDbBean.setPackageName(cursor.getString(cursor.getColumnIndex(AgentDbBean.PACKAGE_NAME)));
            oldAgentDbBean.setInstallCode(cursor.getInt(cursor.getColumnIndex(AgentDbBean.INSTALL_CODE)));
            if(oldAgentDbBean.getInstallCode()==null){
                oldAgentDbBean.setInstallCode(1);
            }
            oldAgentDbBean.setAgent(cursor.getString(cursor.getColumnIndex(AgentDbBean.AGENT)));
        }

        if(oldAgentDbBean!=null){
            Log.e("hongliang","更新老的agent-->"+oldAgentDbBean.toString());
            oldAgentDbBean.setAgent(agentDbBean.getAgent());
            oldAgentDbBean.setInstallCode(oldAgentDbBean.getInstallCode()+1);
            writableDatabase.update(AGENT_TABLE_NAME,oldAgentDbBean.toContentValues(),AgentDbBean.PACKAGE_NAME+"=?",new String[]{oldAgentDbBean.getPackageName()});
            Log.e("hongliang","更新agent-->"+oldAgentDbBean.toString());
        }else{
            writableDatabase.insert(AGENT_TABLE_NAME,null,agentDbBean.toContentValues());
            Log.e("hongliang","存入agent-->"+agentDbBean.toString());
        }
        cursor.close();
        writableDatabase.close();
    }
    public void updateAllAgent(String agent){
        if(agent==null) agent="";
        SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from " + AGENT_TABLE_NAME + " limit 1", null);
        AgentDbBean oldAgentDbBean=null;
        while (cursor.moveToNext()){
            oldAgentDbBean=new AgentDbBean();
            oldAgentDbBean.setPackageName(cursor.getString(cursor.getColumnIndex(AgentDbBean.PACKAGE_NAME)));
            oldAgentDbBean.setInstallCode(cursor.getInt(cursor.getColumnIndex(AgentDbBean.INSTALL_CODE)));
            if(oldAgentDbBean.getInstallCode()==null){
                oldAgentDbBean.setInstallCode(1);
            }
            oldAgentDbBean.setAgent(cursor.getString(cursor.getColumnIndex(AgentDbBean.AGENT)));
        }
        if(oldAgentDbBean!=null&&!agent.equals(oldAgentDbBean.getAgent())){//有并且不一样
            ContentValues cv = new ContentValues();
            cv.put(AgentDbBean.AGENT,agent);
            int update = writableDatabase.update(AGENT_TABLE_NAME, cv, null, null);
            Log.e("hongliang","更新了所有的agent--> count="+update+"  agent="+agent);
        }else{
            Log.e("hongliang","无需更新--> agent="+agent);
        }
        writableDatabase.close();
    }
    public AgentDbBean getAgengDbBeanByPackageName(String packageName){
        SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
        Cursor cursor = readableDatabase.rawQuery("select * from " + AGENT_TABLE_NAME + " where packageName=?", new String[]{packageName});
        AgentDbBean oldAgentDbBean=null;
        while (cursor.moveToNext()){
            oldAgentDbBean=new AgentDbBean();
            oldAgentDbBean.setPackageName(cursor.getString(cursor.getColumnIndex(AgentDbBean.PACKAGE_NAME)));
            oldAgentDbBean.setInstallCode(cursor.getInt(cursor.getColumnIndex(AgentDbBean.INSTALL_CODE)));
            if(oldAgentDbBean.getInstallCode()==null){
                oldAgentDbBean.setInstallCode(1);
            }
            oldAgentDbBean.setAgent(cursor.getString(cursor.getColumnIndex(AgentDbBean.AGENT)));
        }
        return oldAgentDbBean;
    }
}
